package logic

import (
	"context"
	"day3/models/mysqlinit"
	"day3/pkg"
	"fmt"

	"day3/Realrpc/internal/svc"
	"day3/Realrpc/realrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *realrpc.RealNameRequest) (*realrpc.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	var names mysqlinit.RealNameAuth
	err := names.GetRealNameAuthInfo(in.UserId, in.RealName)
	if err != nil {
		logx.Error("实名认证信息错误")
		return nil, fmt.Errorf("实名认证信息错误")
	}
	if names.ID != 0 {
		logx.Error("用户实名已实名，请重新实名")
		return nil, fmt.Errorf("用户实名已实名，请重新实名")
	}
	names.RealName = in.RealName
	names.UserId = in.UserId
	names.IdCord = in.IdCord
	if !pkg.RealName(in.IdCord, in.RealName) {
		logx.Error("第三方实名认证错误")
		return nil, fmt.Errorf("第三方实名认证错误")
	}

	err = names.CreateReal()
	if err != nil {
		logx.Error("保存实名认证失败")
		return nil, fmt.Errorf("保存实名认证失败")
	}

	return &realrpc.RealNameResponse{}, nil
}
